package com.yangluhan.util;

import com.alibaba.druid.pool.DruidPooledConnection;
import com.alibaba.fastjson.JSONObject;
import com.yangluhan.bean.GmallConfig;
import org.apache.commons.lang.StringUtils;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Set;

public class PhoenixUtil {
    public static void upsertValues(DruidPooledConnection connection, String sinkTable, JSONObject data) throws SQLException {
        //编写sql
        Set<String> key = data.keySet();
        Collection<Object> values = data.values();
        String sql = "upsert into "+ GmallConfig.HBASE_SCHEMA+"."+sinkTable+"("+ StringUtils.join(key,",") +") values ('"
                +StringUtils.join(values,"','") + "')";
        //预编译sql
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        //执行sql
        preparedStatement.execute();
        connection.commit();
        //关闭连接
        preparedStatement.close();
    }
}
